package com.inspector.app.ui.login

import android.os.Bundle
import android.text.method.HideReturnsTransformationMethod
import android.text.method.PasswordTransformationMethod
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.CompoundButton
import androidx.fragment.app.Fragment
import androidx.fragment.app.activityViewModels
import com.example.commen.manager.ActivityManager
import com.example.commen.utile.SingleClick
import com.hjq.http.listener.HttpCallback
import com.hjq.toast.Toaster
import com.inspector.app.R
import com.inspector.app.databinding.FragmentUserPwdBinding
import com.inspector.app.di.network.api.SmsloginApi
import com.inspector.app.di.network.api.UserInfoApi
import com.inspector.app.ui.base.BaseDJFragment
import com.inspector.app.ui.main.MainActivity
import com.inspector.app.util.ConstantUtil
import com.inspector.app.util.SharedPreferencesUtils


/**
 * A simple [Fragment] subclass.
 * Use the [UserPwdFragment.newInstance] factory method to
 * create an instance of this fragment.
 */
class UserPwdFragment : BaseDJFragment<FragmentUserPwdBinding>() {
    private val viewModel: LoginModel by activityViewModels()

    override fun provideViewBinding(
        inflater: LayoutInflater,
        container: ViewGroup?
    ): FragmentUserPwdBinding {
        return FragmentUserPwdBinding.inflate(inflater, container, false)
    }

    override fun setupView(view: View, savedInstanceState: Bundle?) {

    }

    override fun initData() {
        viewBinding.cbSavepwd.isChecked = SharedPreferencesUtils.getBoolean(ConstantUtil.REMEMBER_PASSWORD)
        if (viewBinding.cbSavepwd.isChecked){
            val account = SharedPreferencesUtils.getString(ConstantUtil.ACCOUNT)
            val pwd = SharedPreferencesUtils.getString(ConstantUtil.PASSWORD)
            if (!account.isNullOrBlank()) {
                viewBinding.edtLoginName.setText(account)
            }
            if (!pwd.isNullOrBlank()) {
                viewBinding.edtLoginPwd.setText(pwd)
            }

        }
    }

    override fun initListener() {
        viewBinding.cbSavepwd.setOnCheckedChangeListener(CompoundButton.OnCheckedChangeListener { buttonView, isChecked ->
            SharedPreferencesUtils.put(ConstantUtil.REMEMBER_PASSWORD, isChecked)
        })
        SingleClick(viewBinding.ivEyesLogin) {
            setIsShowPassword()
        }
        SingleClick(viewBinding.btLoginGo) {
            val name = viewBinding.edtLoginName.text.toString()
            val pwd = viewBinding.edtLoginPwd.text.toString()
            if (name.isEmpty() || name.isBlank()) {
                Toaster.show("账号不能为空")
                return@SingleClick
            }
            if (pwd.isEmpty() || pwd.isBlank()) {
                Toaster.show("密码不能为空")
                return@SingleClick
            }
            viewModel.Login(this, name, pwd, object : HttpCallback<SmsloginApi.loginResult>(this) {
                override fun onSucceed(result: SmsloginApi.loginResult) {
                    SharedPreferencesUtils.put(ConstantUtil.REMEMBER_PASSWORD, viewBinding.cbSavepwd.isChecked)
                    SharedPreferencesUtils.put(ConstantUtil.ACCOUNT, name)
                    SharedPreferencesUtils.put(ConstantUtil.PASSWORD, pwd)
                    viewModel.getUserInfo(requireActivity(),
                        object : HttpCallback<UserInfoApi.UserInfo>(this) {
                            override fun onSucceed(result: UserInfoApi.UserInfo) {
                                Toaster.show("登录成功")
                                goToActivity(requireActivity(), MainActivity::class.java)
                                ActivityManager.getInstance()
                                    .finishAllActivities(MainActivity::class.java)
                            }

                            override fun onFail(e: java.lang.Exception) {
                                Toaster.show(e.localizedMessage)
                            }
                        })
                }

                override fun onFail(e: Exception) {
                    Toaster.show(e.localizedMessage)
                }
            })
        }

    }

    private var isOpenEyes = false

    /**设置是否显示密码*/
    private fun setIsShowPassword() {
        if (isOpenEyes) {
            isOpenEyes = false
            viewBinding.ivEyesLogin.setImageResource(R.drawable.img_close_eyes)
            viewBinding.edtLoginPwd.transformationMethod =
                PasswordTransformationMethod.getInstance()
        } else {
            isOpenEyes = true
            viewBinding.ivEyesLogin.setImageResource(R.drawable.img_open_eyes)
            viewBinding.edtLoginPwd.transformationMethod =
                HideReturnsTransformationMethod.getInstance()
        }
        // 把光标移到最后
        viewBinding.edtLoginPwd.setSelection(
            viewBinding.edtLoginPwd.text.toString().trim().length
        )
    }

}